package org.liquidByte.simpleChat.server;

import java.util.Collection;
import java.util.HashMap;

import org.liquidByte.simpleChat.communication.Channel;
import org.liquidByte.simpleChat.dto.User;

class UserChannelManager {
	private static HashMap<Channel, UserChannel> channelMap;
	private static HashMap<User, UserChannel> userMap;
	
	static {
		UserChannelManager.channelMap = new HashMap<Channel, UserChannel>();
		UserChannelManager.userMap = new HashMap<User, UserChannel>();
	}
	
	public synchronized static void put(Channel channel, UserChannel userChannel) {
		UserChannelManager.channelMap.put(channel, userChannel);
		UserChannelManager.userMap.put(userChannel.getUser(), userChannel);
	}
	public synchronized static void remove(Channel channel) {
		UserChannel usrChannel = UserChannelManager.channelMap.remove(channel);
		UserChannelManager.userMap.remove(usrChannel.getUser());
	}
	public synchronized static void remove(User user) {
		UserChannel usrChannel = UserChannelManager.userMap.remove(user);
		UserChannelManager.channelMap.remove(usrChannel.getChannel());
	}
	public static UserChannel get(User user) {
		return UserChannelManager.userMap.get(user);
	}
	public static UserChannel get(Channel channel) {
		return UserChannelManager.channelMap.get(channel);
	}
	public static Collection<UserChannel> getActiveUsers() {
		return UserChannelManager.channelMap.values();
	}

	public static UserChannel getCurrent() {
		Channel channel = Channel.getCurrent();
		if (channel == null) {
			return null;
		}
		else {
			return UserChannelManager.get(channel);
		}
	}
}
